package com.sinba.common.business.action;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sinba.common.business.dto.CurrentSaleDTO;
import com.sinba.common.business.dto.MatchDTO;
import com.sinba.common.business.dto.MatchQueryDTO;
import com.sinba.common.business.service.IMatchService;
import com.sinba.common.dto.PageDTO;

@SuppressWarnings("serial")
public class MatchAction extends ActionSupport{
	@Autowired
	private IMatchService matchService;
	private File imgUrl;
	private String imgUrlFileName;		//文件名
	private String imgUrlContentType;	//类型
	private String sord;
    private String sidx;
    private int page = 1;
    private int rows = 10;
    
	private PageDTO pageDTO;
	private MatchDTO matchDTO = new MatchDTO();
	private MatchQueryDTO matchQueryDTO = new MatchQueryDTO();
	private Long[] ids;
	private String resultStr;
	private String pageUrl;
	private Long matchId;
	private CurrentSaleDTO currentSaleDTO = new CurrentSaleDTO();
	public String getSord() {
		return sord;
	}
	public void setSord(String sord) {
		this.sord = sord;
	}
	public String getSidx() {
		return sidx;
	}
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public PageDTO getPageDTO() {
		return pageDTO;
	}
	public void setPageDTO(PageDTO pageDTO) {
		this.pageDTO = pageDTO;
	}
	public MatchDTO getMatchDTO() {
		return matchDTO;
	}
	public void setMatchDTO(MatchDTO matchDTO) {
		this.matchDTO = matchDTO;
	}
	public MatchQueryDTO getMatchQueryDTO() {
		return matchQueryDTO;
	}
	public void setMatchQueryDTO(MatchQueryDTO matchQueryDTO) {
		this.matchQueryDTO = matchQueryDTO;
	}
	public Long[] getIds() {
		return ids;
	}
	public void setIds(Long[] ids) {
		this.ids = ids;
	}
	public String getResultStr() {
		return resultStr;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	public String getPageUrl() {
		return pageUrl;
	}
	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	public Long getMatchId() {
		return matchId;
	}
	public void setMatchId(Long matchId) {
		this.matchId = matchId;
	}
	public CurrentSaleDTO getCurrentSaleDTO() {
		return currentSaleDTO;
	}
	public void setCurrentSaleDTO(CurrentSaleDTO currentSaleDTO) {
		this.currentSaleDTO = currentSaleDTO;
	}
	/**
	 * 
	 * 开始匹配
	 * @return
	 */
	public String satrtMatch(){
		if(pageUrl==null || pageUrl==""){
			pageUrl="index.jsp";
		}
		resultStr = matchService.satrtMatchByGuPiao();
		
		return "optresult"; 
	}
	
	
	/**
	 * 分页查询
	 * @return
	 */
	public String findMatchPage(){
		
		int start = ( page - 1 ) * rows;
		matchQueryDTO.setStart( start );
		matchQueryDTO.setLimit( rows );
		pageDTO = matchService.findMatchPage( matchQueryDTO, sord, sidx );
		
		if ( pageDTO != null )
		{
		    pageDTO.setPage( page );
		    pageDTO.setRows( rows );
		}
		return SUCCESS;
	}
	
	public String payment(){
		if(pageUrl==null || pageUrl==""){
			pageUrl="index.jsp";
		}
		ActionContext ac = ActionContext.getContext();
		   ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT); 
		String filename = ServletActionContext.getRequest().getRealPath("/");
		resultStr = matchService.payment(matchDTO,imgUrl,imgUrlFileName,filename);
		
		return "optresult"; 
	}
	
	public String confirmPay(){
		if(pageUrl==null || pageUrl==""){
			pageUrl="index.jsp";
		}
		resultStr = matchService.confirmPay(matchDTO);
		return "optresult"; 
	}
	public File getImgUrl() {
		return imgUrl;
	}
	public void setImgUrl(File imgUrl) {
		this.imgUrl = imgUrl;
	}
	
	public String getImgUrlFileName() {
		return imgUrlFileName;
	}
	public void setImgUrlFileName(String imgUrlFileName) {
		this.imgUrlFileName = imgUrlFileName;
	}
	public String getImgUrlContentType() {
		return imgUrlContentType;
	}
	public void setImgUrlContentType(String imgUrlContentType) {
		this.imgUrlContentType = imgUrlContentType;
	}
	
	public String checkCompute(){
		matchService.checkCompute();
		return SUCCESS;
	}
	
	public String cleanMatch(){
		matchService.cleanMatch(matchId);
		return SUCCESS;
	}
	
	public String notReceive(){
		if(pageUrl==null || pageUrl==""){
			pageUrl="index.jsp";
		}
		resultStr = matchService.notReceive(matchId);
		return "optresult"; 
	}
	
	public String sellSplit(){
		if(pageUrl==null || pageUrl==""){
			pageUrl="index.jsp";
		}
		resultStr = matchService.sellSplit();
		return "optresult"; 
	}
	
	public String getCurrentSale(){
		currentSaleDTO = matchService.getCurrentSale();
		return "currentSaleDTO";
	}
	
}
